package ServerConnection;

import Client.ServerInfo;
import Message.AccountAddFriendInfo;
import com.mysql.cj.xdevapi.DatabaseObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;


/**
 * 该类用来调用ThreadHandleAccountAddFriend，实现循环监听来自客户端的好友申请，并将申请写入数据库
 */
public class AccountAddFriendServer implements Runnable {
    DatabaseServer databaseServer;

    AccountAddFriendServer(DatabaseServer databaseServer) {
        this.databaseServer = databaseServer;
    }

    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_FRIENDREQUEST_PORT);
            System.out.println("添加好友的服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handle = new Thread(new ThreadHandleAccountAddFriend(socket, databaseServer));
                handle.start();
            }
        } catch (IOException e) {
            System.out.println("WARNING:AccountAddFriendServer Init fail,Reason:socket has ioException.");
        }
    }
}


class ThreadHandleAccountAddFriend implements Runnable {
    Socket socket = null;
    AccountAddFriendInfo info;
    boolean res;
    String userId;
    String userPassword;
    DatabaseServer databaseServer;

    public ThreadHandleAccountAddFriend(Socket socket, DatabaseServer databaseServer) {
        super();
        this.socket = socket;
        this.databaseServer = databaseServer;
    }

    @Override

    public void run() {
        try {
            System.out.println("收到用户的登录请求，正在处理");
            //获取从客户端得到的消息，并且执行了消息。
            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            info = (AccountAddFriendInfo) objectInputStream.readObject();
            String userIdInDatabaseFrom = info.getUserIdInDatabaseFrom();
            String userIdInDatabaseTo = info.getUserIdInDatabaseTo();
            Timestamp time = info.getTime();
            String messageInfo = info.getMessageInfo();
            System.out.println("messageInfo:" + messageInfo);
            String statement = String.format("INSERT INTO UserFriendList (Uid1,Uid2) VALUES(\"%s\",\"%s\");", userIdInDatabaseFrom, userIdInDatabaseTo);
//            String statement = String.format("INSERT INTO FriendRequest VALUE ('%s','%s','%s','%s','%d','%d','%d');",
//                    userIdInDatabaseFrom, userIdInDatabaseTo, time, messageInfo, 0, 0, 0);
            res = databaseServer.doExecute(statement);
            statement = String.format("INSERT INTO UserFriendList (Uid1,Uid2) VALUES(\"%s\",\"%s\");", userIdInDatabaseTo, userIdInDatabaseFrom);
            System.out.println("申请好友插入结果：" + res);
            //返还执行的结果到客户端。
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(String.valueOf(res).getBytes(StandardCharsets.UTF_8));

            inputStream.close();
            objectInputStream.close();
            outputStream.close();
            socket.close();


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}